package com.ks.service;

import com.ks.base.BaseService;
import com.ks.dao.*;
import com.ks.entity.*;
import com.ks.util.TimeUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("examPaperService")
public class ExamPaperService<T> extends BaseService<T> {
    private final static Logger log = Logger.getLogger(ExamPaperService.class);

    @Autowired
    private ContentDao<T> contentDao;
    @Autowired
    private ExamPaperDao<T> dao;
    @Autowired
    private ExamDao<T> examDao;
    @Autowired
    private ExamineeDao<T> examineeDao;
    @Autowired
    private ExamPaperDao<T> examPaperDao;
    @Autowired
    private ExamPaperStudentDao<T> examPaperStudentDao;
    @Autowired
    private PaperQuestionDao<T> paperQuestionDao;
    @Autowired
    private PracticeDao<T> practiceDao;
    @Autowired
    private PaperDao<T> paperDao;
    @Autowired
    private PaperQuestiontypeDao<T> paperQuestiontypeDao;
    @Autowired
    private UserDao<T> userDao;
    @Autowired
    private QuestionCopyDao<T> questionCopyDao;

    public ExamPaperDao<T> getDao() {
        return dao;
    }


    public Map<String, Object> examingPage(ExamPaper examPaperEntity, Integer userid) {
        // TODO Auto-generated method stub
        Map<String, Object> context = new HashMap<>();
        // 当前用户的考试
        List<T> examPaperlist = examPaperDao.queryList(examPaperEntity);
        ExamPaper examPaper = (ExamPaper) examPaperlist.get(0);
        Exam exam = (Exam) examDao.queryById(examPaper.getExam_id());
        // 查看学生是已经答题
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("exam_id", examPaperEntity.getExam_id());
        userMap.put("paper_id", examPaperEntity.getPaper_id());
        userMap.put("user_id", userid);
        List<T> examPaperStudentList = examPaperStudentDao.queryList(userMap);
        Map<String, Object> questionMap = new HashMap<>();
        questionMap.put("paper_id", examPaper.getPaper_id());

        ApplicationContext ac;
        ExamPaperStudent examPaperStudent;
        ApplicationContext acoption;
        // 为每一位考生生成自己的随机题目顺序
        if (examPaperStudentList.size() == 0) {
            //打乱题目顺序
            if ("1".equals(exam.getQuestionrandom())) {
                // 试卷对应的题 paperquestion表
                Set<PaperQuestion> paperQuestionset = paperQuestionDao.querySet(questionMap);
                int i = 0;
                for (PaperQuestion entity : paperQuestionset) {
                    ac = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
                    examPaperStudent = (ExamPaperStudent) ac.getBean("examPaperStudent");
                    examPaperStudent.setExam_id(examPaperEntity.getExam_id());
                    examPaperStudent.setPaper_id(examPaper.getPaper_id());
                    examPaperStudent.setQuestion_id(entity.getQuestion_copy_id());
                    examPaperStudent.setOrderNum(i+1);
                    examPaperStudent.setUser_id(userid);
                    //查看学生试卷中是否有该题
                    List<T> ts = examPaperStudentDao.queryList(examPaperStudent);
                    if (ts.size()==0) {
                        examPaperStudentDao.add(examPaperStudent);
                        if ("1".equals(exam.getOptionrandom())) {
                            //打乱选项顺序
                            // 查找optioncopy表
                            QuestionCopy questionCopy = (QuestionCopy) questionCopyDao.queryById(entity.getQuestion_copy_id());
                            if (questionCopy.getQuestiontype_id()==3) {
                                List<Map<String, String>> listTemp = new ArrayList<>();
                                Map<String, String> optionMapA = new HashMap<>();
                                optionMapA.put("title",questionCopy.getA());
                                optionMapA.put("picture",questionCopy.getApic());
                                listTemp.add(optionMapA);
                                Map<String, String> optionMapB = new HashMap<>();
                                optionMapB.put("title",questionCopy.getB());
                                optionMapB.put("picture",questionCopy.getBpic());
                                listTemp.add(optionMapB);
                                Collections.shuffle(listTemp);
                                examPaperStudent.setA(listTemp.get(0).get("title"));
                                examPaperStudent.setApic(listTemp.get(0).get("picture"));
                                examPaperStudent.setB(listTemp.get(1).get("title"));
                                examPaperStudent.setBpic(listTemp.get(1).get("picture"));
                            } else {
                                List<Map<String, String>> listTemp = new ArrayList<>();
                                Map<String, String> optionMapA = new HashMap<>();
                                optionMapA.put("title",questionCopy.getA());
                                optionMapA.put("picture",questionCopy.getApic());
                                listTemp.add(optionMapA);
                                Map<String, String> optionMapB = new HashMap<>();
                                optionMapB.put("title",questionCopy.getB());
                                optionMapB.put("picture",questionCopy.getBpic());
                                listTemp.add(optionMapB);
                                Map<String, String> optionMapC = new HashMap<>();
                                optionMapC.put("title",questionCopy.getC());
                                optionMapC.put("picture",questionCopy.getCpic());
                                listTemp.add(optionMapC);
                                Map<String, String> optionMapD = new HashMap<>();
                                optionMapD.put("title",questionCopy.getD());
                                optionMapD.put("picture",questionCopy.getDpic());
                                listTemp.add(optionMapD);
                                Collections.shuffle(listTemp);
                                examPaperStudent.setA(listTemp.get(0).get("title"));
                                examPaperStudent.setApic(listTemp.get(0).get("picture"));
                                examPaperStudent.setB(listTemp.get(1).get("title"));
                                examPaperStudent.setBpic(listTemp.get(1).get("picture"));
                                examPaperStudent.setC(listTemp.get(2).get("title"));
                                examPaperStudent.setCpic(listTemp.get(2).get("picture"));
                                examPaperStudent.setD(listTemp.get(3).get("title"));
                                examPaperStudent.setDpic(listTemp.get(3).get("picture"));
                            }

                            examPaperStudentDao.updateBySelective(examPaperStudent);
                    }

                    } else {
                        QuestionCopy questionCopy = (QuestionCopy) questionCopyDao.queryById(entity.getQuestion_copy_id());
                        examPaperStudent.setA(questionCopy.getA());
                        examPaperStudent.setApic(questionCopy.getApic());
                        examPaperStudent.setB(questionCopy.getB());
                        examPaperStudent.setBpic(questionCopy.getBpic());
                        examPaperStudent.setC(questionCopy.getC());
                        examPaperStudent.setCpic(questionCopy.getCpic());
                        examPaperStudent.setD(questionCopy.getD());
                        examPaperStudent.setDpic(questionCopy.getDpic());
                        examPaperStudentDao.updateBySelective(examPaperStudent);
                    }
                }
            } else {
                // 试卷对应的题 paperquestion表
                List<PaperQuestion> paperQuestionlist = (List<PaperQuestion>) paperQuestionDao.queryList(questionMap);
                int i = 0;
                for (PaperQuestion entity : paperQuestionlist) {
                    ac = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
                    examPaperStudent = (ExamPaperStudent) ac.getBean("examPaperStudent");
                    examPaperStudent.setExam_id(examPaperEntity.getExam_id());
                    examPaperStudent.setPaper_id(examPaper.getPaper_id());
                    examPaperStudent.setQuestion_id(entity.getQuestion_copy_id());
                    examPaperStudent.setOrderNum(i+1);
                    examPaperStudent.setUser_id(userid);
                    List<T> ts = examPaperStudentDao.queryByList(examPaperStudent);
                    if (ts.size()==0) {
                        examPaperStudentDao.add(examPaperStudent);
                        if ("1".equals(exam.getOptionrandom())) {
                            //打乱选项顺序
                            // 查找optioncopy表
                            QuestionCopy questionCopy = (QuestionCopy) questionCopyDao.queryById(entity.getQuestion_copy_id());
                            if (questionCopy.getQuestiontype_id()==3) {
                                List<Map<String, String>> listTemp = new ArrayList<>();
                                Map<String, String> optionMapA = new HashMap<>();
                                optionMapA.put("title",questionCopy.getA());
                                optionMapA.put("picture",questionCopy.getApic());
                                listTemp.add(optionMapA);
                                Map<String, String> optionMapB = new HashMap<>();
                                optionMapB.put("title",questionCopy.getB());
                                optionMapB.put("picture",questionCopy.getBpic());
                                listTemp.add(optionMapB);
                                Collections.shuffle(listTemp);
                                examPaperStudent.setA(listTemp.get(0).get("title"));
                                examPaperStudent.setApic(listTemp.get(0).get("picture"));
                                examPaperStudent.setB(listTemp.get(1).get("title"));
                                examPaperStudent.setBpic(listTemp.get(1).get("picture"));
                            } else {
                                List<Map<String, String>> listTemp = new ArrayList<>();
                                Map<String, String> optionMapA = new HashMap<>();
                                optionMapA.put("title",questionCopy.getA());
                                optionMapA.put("picture",questionCopy.getApic());
                                listTemp.add(optionMapA);
                                Map<String, String> optionMapB = new HashMap<>();
                                optionMapB.put("title",questionCopy.getB());
                                optionMapB.put("picture",questionCopy.getBpic());
                                listTemp.add(optionMapB);
                                Map<String, String> optionMapC = new HashMap<>();
                                optionMapC.put("title",questionCopy.getC());
                                optionMapC.put("picture",questionCopy.getCpic());
                                listTemp.add(optionMapC);
                                Map<String, String> optionMapD = new HashMap<>();
                                optionMapD.put("title",questionCopy.getD());
                                optionMapD.put("picture",questionCopy.getDpic());
                                listTemp.add(optionMapD);
                                Collections.shuffle(listTemp);
                                examPaperStudent.setA(listTemp.get(0).get("title"));
                                examPaperStudent.setApic(listTemp.get(0).get("picture"));
                                examPaperStudent.setB(listTemp.get(1).get("title"));
                                examPaperStudent.setBpic(listTemp.get(1).get("picture"));
                                examPaperStudent.setC(listTemp.get(2).get("title"));
                                examPaperStudent.setCpic(listTemp.get(2).get("picture"));
                                examPaperStudent.setD(listTemp.get(3).get("title"));
                                examPaperStudent.setDpic(listTemp.get(3).get("picture"));
                            }
                            examPaperStudentDao.updateBySelective(examPaperStudent);
                        } else {
                            QuestionCopy questionCopy = (QuestionCopy) questionCopyDao.queryById(entity.getQuestion_copy_id());
                            examPaperStudent.setA(questionCopy.getA());
                            examPaperStudent.setApic(questionCopy.getApic());
                            examPaperStudent.setB(questionCopy.getB());
                            examPaperStudent.setBpic(questionCopy.getBpic());
                            examPaperStudent.setC(questionCopy.getC());
                            examPaperStudent.setCpic(questionCopy.getCpic());
                            examPaperStudent.setD(questionCopy.getD());
                            examPaperStudent.setDpic(questionCopy.getDpic());
                            examPaperStudentDao.updateBySelective(examPaperStudent);
                        }
                    }
                }
            }
        }
        context = getResultMap(examPaperEntity.getExam_id(), examPaper.getPaper_id(), userid);
        context.put("examid", examPaperEntity.getExam_id());
        context.put("paperid", examPaper.getPaper_id());
        context.put("userid", userid);
        ApplicationContext acexaminee = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
        Examinee examinee  = (Examinee) acexaminee.getBean("examinee");
        examinee.setExam_id(examPaperEntity.getExam_id());
        examinee.setUser_id(userid);
        List<T> list = examineeDao.queryList(examinee);
        Examinee entity = (Examinee) list.get(0);
        if (null==entity.getStarttime()){
            entity.setStarttime(new Date());
            examineeDao.update(entity);
        }
        //时间处理类
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTime =System.currentTimeMillis();
        //从对象中拿到时间
        long createTime;
        try {
            createTime = df.parse(df.format(entity.getStarttime())).getTime();
            long diff=(currentTime-createTime)/1000;//秒
            int duration = Integer.parseInt(exam.getDuration());
            context.put("minitues", (duration*60-diff)/60);
            context.put("timeleft", duration*60-diff);
            System.out.println("当前系统时间为："+currentTime+"下单时间为："+createTime+"两个时间差为："+diff);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return context;
    }

    /**
     *  判断考试时间是不是小于30分钟
     * @param user
     * @return
     */
    public Exam waitingPage(User user) {
        Exam  examEntity = null;
        List<Examinee> examinees =  examineeDao.findByUserId(user.getId());
        if(examinees.size()==0){
            return null;
        }
        List<Integer> examIds = new ArrayList<>();
        for (Examinee examinee : examinees) {
            examIds.add(examinee.getExam_id());
        }
        List<Exam> exams = examDao.findByIds(examIds);
        List<Exam> temp = new ArrayList<>();
        String timeleft = "";
        String timeleftsecond = "";
        for (Exam exam : exams) {
            if ("1".equals(exam.getStatus())) {
                if (null==exam.getStarttime()) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("user_id",user.getId());
                    map.put("exam_id",exam.getId());
                    List<Examinee> examineelist = (List<Examinee>) examineeDao.queryList(map);
                    if (examineelist.size()>0) {
                        if (null==examineelist.get(0).getScore()) {
                            return exam;
                        }
                    }
                } else {
                    long date = new Date().getTime();//当前时间
                    long time = exam.getStarttime().getTime();//考试时间
                    long examTime = Long.parseLong(exam.getDuration())*60000;//考试时长
                    // 开考前半小时
                    if((time-date)/60000<30 && (time-date)/60000>=0){
                        temp.add(exam);
                    }else {
                        //考试开始还没结束
                        if(time+examTime-date>0 && time<date){
                            temp.add(exam);
                        }
                    }

                    //时间处理类
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    long currentTime =System.currentTimeMillis();
                    //从对象中拿到时间
                    long createTime;
                    try {
                        int tempTime = 0;
                        long diff=(time-currentTime)/1000;//秒
                        tempTime = (int)diff;
                        timeleftsecond = tempTime+"";
                        timeleft =  TimeUtil.getTimeString(tempTime);
                        timeleft = "00:"+timeleft;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (temp.size()>0) {
            Map<String, Object> map = new HashMap<>();
            map.put("user_id",user.getId());
            for (Exam exam : temp) {
                map.put("exam_id",exam.getId());
                examEntity = (Exam) examDao.queryById(exam.getId());
                List<Examinee> examineelist = (List<Examinee>) examineeDao.queryList(map);
                if (examineelist.size()>0) {
                    if (null==examineelist.get(0).getScore()) {
                        examEntity.setExtend(timeleft);
                        examEntity.setExtend2(timeleftsecond);
                        return examEntity;
                    }
                }
            }
        }
        return examEntity;
    }

    @Transactional
    public Map<String, Object> calculation(ExamPaperStudent entity) {
        // TODO Auto-generated method stub
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> questionMap = new HashMap<>();
        questionMap.put("exam_id",entity.getExam_id());
        questionMap.put("paper_id",entity.getPaper_id());
        questionMap.put("user_id",entity.getUser_id());
        //查询paper表
        Paper paper = (Paper) paperDao.queryById(entity.getPaper_id());
        User user = (User) userDao.queryById(entity.getUser_id());
        resultMap.put("paper", paper);
        resultMap.put("user", user);
        Map<String, Object> singlemap = new HashMap<>();
        Map<String, Object> judgemap = new HashMap<>();
        Map<String, Object> mutimap = new HashMap<>();
        int singlecorr = 0;
        int muticorr = 0;
        int judgecorr = 0;
        List<Map<String, String>> list = examPaperStudentDao.queryPaperUserQuestions(questionMap);

        for (Map<String, String> map : list) {
            boolean bool = "1".equals(String.valueOf(map.get("typeid")));
            //多项选择题
            // 问题选项
            Map<String, String> qcMap =  questionCopyDao.queryByIdMap(map.get("question_id"));
            if ("1".equals(String.valueOf(map.get("typeid")))) {  //单选题
                String myoptionkey = map.get("answer").toLowerCase();
                String myoptionvalue = map.get(myoptionkey)+"";
                String answerkey = qcMap.get("answer").toLowerCase();
                String answervalue = qcMap.get(answerkey)+"";
                if (myoptionvalue.equals(answervalue)) {
                    singlecorr = singlecorr + 1;
                }
            } else if ("2".equals(String.valueOf(map.get("typeid")))) {   //多选题
                String answerkey = qcMap.get("answer").toLowerCase();
                String answervalue = "";
                Set<String> answerSet = new HashSet<>();
                for(int i=0;i<answerkey.length();i++) {
                    char c =answerkey.charAt(i);
                    answervalue = qcMap.get(c+"");
                    answerSet.add(answervalue);
                }
                int temp = answerSet.size();
                String myoptionkey = map.get("answer").toLowerCase();
                String myoptionvalue = "";
                for(int i=0;i<myoptionkey.length();i++) {
                    char c =myoptionkey.charAt(i);
                    myoptionvalue = map.get(c+"");
                    answerSet.add(myoptionvalue);
                }
                if (answerkey.length()==myoptionkey.length()&&answerSet.size()==temp) {
                    muticorr = muticorr + 1;
                }
            } else {  //判断题
                String myoptionkey = map.get("answer").toLowerCase();
                String myoptionvalue = map.get(myoptionkey)+"";
                String answerkey = qcMap.get("answer").toLowerCase();
                String answervalue = qcMap.get(answerkey)+"";
                if (myoptionvalue.equals(answervalue)) {
                    judgecorr = judgecorr + 1;
                }
            }
        }
        Map<String, Object> typeMap = new HashMap<>();
        typeMap.put("paper_id", entity.getPaper_id());
        typeMap.put("questiontype_id", 1);
        int singlescore = getMap(singlemap, singlecorr, typeMap);
        typeMap.put("questiontype_id", 3);
        int judgescore = getMap(judgemap, judgecorr, typeMap);
        typeMap.put("questiontype_id", 2);
        int mutiscore = getMap(mutimap, muticorr, typeMap);
        resultMap.put("single", singlemap);
        resultMap.put("judge", judgemap);
        resultMap.put("muti", mutimap);
        int singlegoals = singlecorr * singlescore;
        int judgecorrgoals = judgecorr * judgescore;
        int mutigoals = muticorr * mutiscore;
        int total = singlegoals + judgecorrgoals + mutigoals;
        resultMap.put("score", total);
        //保存考生成绩
        Map<String, Object> updateMap = new HashMap<>();
        updateMap.put("score", total);
        updateMap.put("single", singlegoals);
        updateMap.put("judge", judgecorrgoals);
        updateMap.put("muti", mutigoals);
        updateMap.put("exam_id", entity.getExam_id());
        updateMap.put("user_id", entity.getUser_id());
        examineeDao.updateByExamidAndUserid(updateMap);
        return resultMap;
    }

    private int getMap(Map<String, Object> map, int corr, Map<String, Object> typeMap) {
        List<T> typelist;
        PaperQuestiontype entitytype;
        typelist = paperQuestiontypeDao.queryList(typeMap);
        if (typelist.size()>0) {
            entitytype = (PaperQuestiontype) typelist.get(0);
            int score = Integer.parseInt(entitytype.getScore());
            int count = Integer.parseInt(entitytype.getCount());
            map.put("score", score);
            map.put("count", count);
            map.put("corrCount", corr);
            return score;
        }
        return 0;
    }

    public List<Examinee> queryResults(String id) {
        List<Examinee> list = examineeDao.queryResults(id);
        return list;
    }

    public Map<String, Object> queryUserPaperResults(String examid, String paperid, String studentid) {
        Map<String, Object> resultMap = getResultMap(examid, paperid, studentid);
        Map<String, Object> map = new HashMap<>();
        map.put("exam_id",examid);
        map.put("paper_id",paperid);
        map.put("user_id",studentid);
        map.put("questiontype_id",1);
        List<Map<String, String>> singlelist =  examPaperStudentDao.queryPaperUserQuestionsAnswer(map);
        for (Map<String, String> stringObjectMap : singlelist) {
            String answerkey = stringObjectMap.get("answercorr").toLowerCase();
            String answervalue = stringObjectMap.get(answerkey);
            String answer = "";
            if(stringObjectMap.get("a").equals(answervalue)){
                answer = "A";
            } else if(stringObjectMap.get("b").equals(answervalue)){
                answer = "B";
            } else if(stringObjectMap.get("c").equals(answervalue)){
                answer = "C";
            } else {
                answer = "D";
            }
            stringObjectMap.put("answer",answer);
        }

        resultMap.put("singlelist", singlelist);
        map.put("questiontype_id",2);
        List<Map<String, String>> mutilist =  examPaperStudentDao.queryPaperUserQuestionsAnswer(map);
        for (Map<String, String> stringObjectMap : mutilist) {
            String answerkey = stringObjectMap.get("answercorr").toLowerCase();
            String answer = "";
            for(int i=0;i<answerkey.length();i++) {
                char c =answerkey.charAt(i);
                String single = stringObjectMap.get(c+"");
                String keyStr = "";
                if(stringObjectMap.get("a").equals(single)){
                    keyStr = "A";
                } else if(stringObjectMap.get("b").equals(single)){
                    keyStr = "B";
                } else if(stringObjectMap.get("c").equals(single)){
                    keyStr = "C";
                } else {
                    keyStr = "D";
                }
                answer = answer + keyStr;
            }
            stringObjectMap.put("answer",sortByletter(answer));
        }
        resultMap.put("mutilist", mutilist);
        map.put("questiontype_id",3);
        List<Map<String, String>> judgelist =  examPaperStudentDao.queryPaperUserQuestionsAnswer(map);
        for (Map<String, String> stringObjectMap : judgelist) {
            String answerkey = stringObjectMap.get("answercorr").toLowerCase();
            String answervalue = stringObjectMap.get(answerkey);
            String answer = "";
            if(stringObjectMap.get("a").equals(answervalue)){
                answer = "A";
            } else {
                answer = "B";
            }
            stringObjectMap.put("answer",answer);
        }
        resultMap.put("judgelist", judgelist);
        Exam exam = (Exam) examDao.queryById(examid);
        Map<String, Object> examineeMap = new HashMap<>();
        examineeMap.put("exam_id", examid);
        examineeMap.put("user_id", studentid);
        List<T> list = examineeDao.queryList(examineeMap);
        Examinee examinee = (Examinee) list.get(0);
        System.out.println(examinee);
        Map<String, Object> examidtemp = new HashMap<>();
        examidtemp.put("exam_id", examid);
        List<T> examPaperlist = examPaperDao.queryList(examidtemp);
        Map<String, Object> paperidtemp = new HashMap<>();
        ExamPaper examPaper = (ExamPaper) examPaperlist.get(0);
        paperidtemp.put("paper_id", examPaper.getPaper_id());
        List<T> paperTypelist = paperQuestiontypeDao.queryList(paperidtemp);
        for (T t : paperTypelist) {
            PaperQuestiontype entity = (PaperQuestiontype) t;
            if (entity.getQuestiontype_id() == 1) {
                resultMap.put("sincorr", Integer.parseInt(examinee.getSingle()) / Integer.parseInt(entity.getScore()));
            } else if (entity.getQuestiontype_id() == 3) {
                resultMap.put("judcorr", Integer.parseInt(examinee.getJudge()) / Integer.parseInt(entity.getScore()));
            } else {
                resultMap.put("muticorr", Integer.parseInt(examinee.getMuti()) / Integer.parseInt(entity.getScore()));
            }
        }
        resultMap.put("examinee", examinee);
        resultMap.put("examName", exam.getName());
        return resultMap;
    }

    public Map<String, Object> getResultMap(Object examid, Object paperid, Object userid) {
        int questions = 0;
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> questionMap = new HashMap<>();
        questionMap.put("exam_id", examid);
        questionMap.put("paper_id", paperid);
        questionMap.put("user_id", userid);
        questionMap.put("questiontype_id", 1);
        //每道题的分值
        List<T> scoresinglelist = paperQuestiontypeDao.queryList(questionMap);
        if (scoresinglelist.size()>0) {
            PaperQuestiontype scoresingle = (PaperQuestiontype) scoresinglelist.get(0);
            List<Map<String, String>> singlelist = examPaperStudentDao.queryPaperUserQuestions(questionMap);
            resultMap.put("singlelist", singlelist);
            resultMap.put("singlecount", singlelist.size());
            resultMap.put("singlescore", scoresingle.getScore());
            questions = questions + singlelist.size();
        }
        questionMap.put("questiontype_id", 2);
        //每道题的分值
        List<T> scoremutilist = paperQuestiontypeDao.queryList(questionMap);
        if (scoremutilist.size()>0) {
            PaperQuestiontype scoremuti = (PaperQuestiontype) scoremutilist.get(0);
            List<Map<String, String>> mutilist = examPaperStudentDao.queryPaperUserQuestions(questionMap);
            resultMap.put("mutilist", mutilist);
            resultMap.put("muticount", mutilist.size());
            resultMap.put("mutiscore", scoremuti.getScore());
            questions = questions + mutilist.size();
        }
        questionMap.put("questiontype_id", 3);
        List<T> scorejudgelist = paperQuestiontypeDao.queryList(questionMap);
        if (scoremutilist.size()>0) {
            PaperQuestiontype scorejudge = (PaperQuestiontype) scorejudgelist.get(0);
            List<Map<String, String>> judgelist = examPaperStudentDao.queryPaperUserQuestions(questionMap);
            resultMap.put("judgelist", judgelist);
            resultMap.put("judgecount", judgelist.size());
            resultMap.put("judgescore", scorejudge.getScore());
            questions = questions + judgelist.size();
        }
        resultMap.put("questions", questions);
        return resultMap;
    }

    public Map<String, Object> practicelistPage(User user) {

        Map<String, Object> resultMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("pro_id", user.getPro_id());
        map.put("rank_id", user.getRank_id());
        List<T> list = contentDao.queryList(map);
        Map<String, Object> countMap = new HashMap<>();
        countMap.put("user_id", user.getId());
        countMap.put("questiontype_id", 1);
        List<Practice> singlelist = practiceDao.queryListByUseridType(countMap);

        countMap.put("user_id", user.getId());
        countMap.put("questiontype_id", 3);
        List<Practice> judgelist = practiceDao.queryListByUseridType(countMap);

        countMap.put("user_id", user.getId());
        countMap.put("questiontype_id", 2);
        List<Practice> mutilist = practiceDao.queryListByUseridType(countMap);
        resultMap.put("single", singlelist.size());
        resultMap.put("judge", judgelist.size());
        resultMap.put("muti", mutilist.size());
        resultMap.put("list", list);
        return resultMap;
    }
    /***
     * 字符串按照字母排序
     */
  private static String sortByletter(String string) {
      char temp;
      char[] str = string.toCharArray();
      for (int i = 0; i < string.length(); i++) {
          for (int j = string.length() - 1; j > i; j--) {
              if (str[j] <= str[j - 1]) {
                  temp = str[j];
                  str[j] = str[j - 1];
                  str[j - 1] = temp;
              }
          }
      }
      return String.valueOf(str);
  }


}
